const app = getApp();
Page({
  data: {
    index: null, //仓库下标
    material_index: null, //物料下标
    colors_index: null, //颜色索引
    warehouse: [], //仓库
    material_list: [], //物料名称
    color_list: [], //颜色
    quantity: '', //数量
    date: '2018-12-25', //初始化时间
    use_id: '', //获取的token
    tokens: '', //获取的id
    warehouse_id: '', //仓库id
    material_id: '', //物料id
    color_id: '', //颜色id
    start_time:'',
  },

  onLoad() {
    var user_id = wx.getStorageSync('user_id')
    var token = wx.getStorageSync('token')
    this.setData({
      use_id: user_id,
      tokens: token,
    })
    this.getwarehouse();
  },
  onShow() {
    this.getdate() //最大选择的日期（当天）
  },
  PickerChange(e) {
    var select = e.currentTarget.dataset.select
    var arr_index = Number(e.detail.value)
    // console.log(arr_index);
    // //选择种类
    if (select == 1) {
      this.setData({
        index: Number(arr_index)
      })
      // console.log("仓库id", this.data.warehouse[this.data.index].id);
      this.setData({
        warehouse_id: this.data.warehouse[this.data.index].id //仓库id
      })
      this.getmaterial_id(this.data.tokens, this.data.use_id, this.data.warehouse[this.data.index].id) //根据仓库id查询物料
    } else if (select == 2) {
      this.setData({
        material_index: arr_index
      })
      // console.log("物料名称id", this.data.material_list[this.data.material_index].id);
      this.setData({
        material_id: this.data.material_list[this.data.material_index].id
      })
      // console.log("物料名称", this.data.material_list[this.data.material_index].name);
      var material_id = this.data.material_list[this.data.material_index].id
      this.getcolor_id(this.data.tokens, this.data.use_id, material_id) //根据物料id查询颜色

    } else if (select == 3) {
      this.setData({
        colors_index: arr_index
      })
      // console.log("物料详细规格id", this.data.color_list[this.data.colors_index].id);
      this.setData({
        color_id: this.data.color_list[this.data.colors_index].id
      })
      // console.log("物料详细规格颜色", this.data.color_list[this.data.colors_index].color);


    }

  },
  //登陆时展示的查询页面
  getwarehouse() {
    wx.request({
      url: `${app.globalData.api}/api/User/warehouse_list`,
      method: 'POST',
      data: {
        token: this.data.tokens,
        user_id: this.data.use_id,
      },
      success: (res) => {
        // console.log(res.data.data);
        var allwarehouse = res.data.data;
        this.setData({
          warehouse: allwarehouse
        })
      }
    })
  },

  //入库按钮
  formSubmit(e) {
    // console.log('form发生了submit事件，携带数据为：', e.detail.value)
    // this.show();
    var warehouse_data = e.detail.value
    console.log(warehouse_data);
    // console.log(this.data.warehouse_id);
    // console.log(this.data.material_id);
    // console.log(this.data.color_id);
    if (warehouse_data.date == '' || this.data.warehouse_id == '' || this.data.material_id == '' || warehouse_data.material_name == '' || this.data.color_id == '' || warehouse_data.color == '' || warehouse_data.quantity == '') {
      wx.showToast({
        title: '还有未选择信息',
        icon: 'error',
        duration: 1500
      })
    } else {
      wx.request({
        url: `${app.globalData.api}/api/User/warehouse_ru`,
        method: 'POST',
        data: {
          token: this.data.tokens, //token
          user_id: this.data.use_id, //
          ru_time: warehouse_data.date, //入库时间
          chu_time: warehouse_data.date, //入库时间
          ware_id: this.data.warehouse_id, //仓库id
          material_id: this.data.material_id, //物料名称id
          material_name: warehouse_data.material_name, //物料名称
          warehouse_info_id: this.data.color_id, //物料详细规格id
          color: warehouse_data.color, //物料详细规格颜色
          count: warehouse_data.quantity, //数量
        },
        success: (res) => {
          // console.log(res);
       
          if(res.data.code==200){
            this.show() //显示
            var that = this
            setTimeout(function () { //两秒后隐藏
              that.close()
            }, 2000)
            setTimeout(function(){
              that.router()
            },1000)
          }else{
            wx.showToast({
              title: '入库失败请重试',
              icon: 'error',
              duration: 1500
            })
          }
        }
      })
    }

  },
  router() {
    wx.switchTab({
      url: '../select/select',
    })
  },
  //根据仓库id查物料
  getmaterial_id(tokens, use_id, id) {
    wx.request({
      url: `${app.globalData.api}/api/User/material_list`,
      method: 'POST',
      data: {
        token: tokens,
        user_id: use_id,
        ware_id: id
      },
      success: (res) => {
        console.log("选择的仓库", res.data.data);
        if (res.data.data == "") {
          wx.showToast({
            title: '该仓库下无物料',
            icon: 'error',
            duration: 1500
          })
          this.setData({
            material_id:false,
            material_list:null,
          })
        } else {
          this.setData({
            material_list: res.data.data
          })
        }

      }
    })
  },
  //根据物料id查询颜色
  getcolor_id(tokens, use_id, id) {
    wx.request({
      url: `${app.globalData.api}/api/User/material_color_list`,
      method: 'POST',
      data: {
        token: tokens,
        user_id: use_id,
        material_id: id
      },
      success: (res) => {
        console.log("选择的物料", res.data.data);
        if (res.data.data == "") {
          wx.showToast({
            title: '该物料下无颜色',
            icon: 'error',
            duration: 1500
          })
          this.setData({
            color_id:false,
            color_list:null,
          })
        } else {
          this.setData({
            color_list: res.data.data
          })
        }

      }
    })
  },

  //入库日期
  DateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },

  // 遮罩层显示
  show() {
    this.setData({
      falg: true
    })
  },
  // 遮罩层隐藏
  close() {
    this.setData({
      falg: false
    })
  },
  //获取当前日期
  getdate() {
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    //获取年份  
    var Y = date.getFullYear();
    //获取月份  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //获取当日日期 
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    var nowdate = Y + '-' + M + '-' + D
    // console.log(nowdate);
    this.setData({
      end_time: nowdate,
      start_time:nowdate
    })
  },
  //如果不是数字不会出现在输入框
  bindinputWeight(event) {
    // console.log(event);
    let value=this.validateNumber(event.detail.value)
    console.log(value);
   this.setData({
     quantity:value
   })
  //  if(this.data.quantity.length>=9){
  //   wx.showToast({
  //     title: '不能超过最大限制',
  //     icon: 'error',
  //     duration: 1500
  //   })
  //  }
  },
  //数字限制
  validateNumber(val) {
    if(val.replace(/\D/g, '')){
      if(val.replace(/\D/g, '').length>=9){
        wx.showToast({
          title: '不能超过最大限制',
          icon: 'error',
          duration: 1500
        })
       }
    }else{
      wx.showToast({
        title: '请输入数字',
        icon: 'error',
        duration: 1500
      })
    }
    return val.replace(/\D/g, '')
  },
})